﻿using Inovout.Cloud.Applications.Security.Models;
using Inovout.Cloud.Applications.Security.Services;
using Inovout.Cloud.DatabaseCluster.Models;
using Inovout.Cloud.DatabaseCluster.Service;
using Inovout.Cloud.Web.Mvc;
using Inovout.Runtime;
using Inovout.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Inovout.Cloud.DatabaseCluster.Console.Web.Controllers
{
    public class DatabaseController : CloudController
    {
        protected ApplicationUser ApplicationUser { get; private set; }
        protected override void Initialize()
        {
            base.Initialize();

            if (Project != null)
            {
                ApplicationUser = ComponentRegistry.Resolve<IApplicationUserService>().FindByApplication((Inovout.Cloud.Applications.Models.Application)base.Project.Applications.ToArray()[0]);
            }
        }
        //
        // GET: /CloudDataBase/
        public ActionResult Configure()
        {
            List<SelectListItem> selectList = new List<SelectListItem>();
            var enumType = typeof(Inovout.Cloud.DatabaseCluster.Service.DriveType);
            foreach (int i in Enum.GetValues(enumType))
            {
                SelectListItem item = new SelectListItem();
                item.Text = Enum.GetName(enumType, i);
                item.Value = i.ToString();
                selectList.Add(item);
            }
            Inovout.Cloud.DatabaseCluster.Service.DatabaseService databaseService = (Inovout.Cloud.DatabaseCluster.Service.DatabaseService)ComponentRegistry.Resolve<Inovout.Cloud.DatabaseCluster.Service.IDatabaseService>();
            List<Inovout.Cloud.DatabaseCluster.Models.Database> databaseList = (List<Inovout.Cloud.DatabaseCluster.Models.Database>)databaseService.FindAllByProject(Project);
            ViewBag.DatabaseList = databaseList;
            ViewBag.SelectList = selectList;
            return View();
        }

        [HttpPost]
        public ActionResult Save(Database database)
        {
            Inovout.Cloud.DatabaseCluster.Service.DatabaseService databaseService = (Inovout.Cloud.DatabaseCluster.Service.DatabaseService)ComponentRegistry.Resolve<Inovout.Cloud.DatabaseCluster.Service.IDatabaseService>();
            database.Project = Project;
            databaseService.Save(database);
            return RedirectToAction("Configure", new { projectId = Project.Id });
        }

        public ActionResult DownFile(string fileName)
        {
            var filePath = Server.MapPath(ComponentRegistry.Resolve<IApplication>().Settings["AttachmentPath"].ToString() + "/" + fileName);
            FileStream fs = new FileStream(filePath, FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            Response.Charset = "UTF-8";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            Response.ContentType = "application/octet-stream";

            Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fileName));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
            return new EmptyResult();

        }
    }
}